<template>
	<view class="Recharge_content Transfer_content">
		<view class="kuang">
			<view class="title">
				<view class="inline-block left">
					<image src="../../static/image/left.png" mode="" @tap="back"></image>
				</view>
				<view class="inline-block titleText" :style="i18n.titleTextstyle">
					{{i18n.titleText}}
				</view>
			</view>
			
			<view class="choice" style="margin-top: 57upx;">
				<view class="chose_coin">
						<image :src="titleimg"></image>
					<text>{{symbol}}</text>
				</view>
				<view class="picker">
					<text>{{accountName}}</text>
					<!-- <picker @change="bindPickerChange" :value="index" :range="array">
							<view class="uni-input">{{array[index]}}</view>
					</picker> -->
					<image src="../../static/image/bottom.png" class="down" style="width: 24upx;height: 12upx;margin-left: 15upx;"></image>
				</view>
			</view>
			
		
			<view class="out_detial">
				<view class="out_address">
					<text>{{i18n.out_address}}</text>
					<image src="../../static/image/address_icon.png"></image>
				</view>
				<input type="text" class="address_input" 
				v-model="address"
				:placeholder="i18n.Addressaaa" placeholder-style="color:#C2C9DF;font-size:11px;"/>
				
				<view class="out_address" style="margin-top: 40upx;">
					<text>{{i18n.out_addressnum}}</text>
					<text class="out_all" @tap="all">{{i18n.out_all}}</text>
				</view>
				<input type="number" class="address_input" 
				v-model="outAmount"
				:placeholder="i18n.address_input" placeholder-style="color:#C2C9DF;font-size:11px;"/>
				<view class="out_price">
					<!-- <view>矿工费：0.01 {{symbol}}</view> -->
					<view>{{i18n.out_price}}：{{amount}} {{symbol}}</view>
				</view>
				<view class="out_address">
					<text>{{i18n.TransferNotes}}</text>
				</view>
				<textarea auto-height class="textarea" v-model="remark" :placeholder="i18n.Optional" placeholder-class="remarkPlaceholderStyle"/>
			</view>
			
			<view class="tips">*{{i18n.tips}}</view>
			
		<image :src="i18n.outbtn" class="out-btn" @tap="outSubmit"></image>
			
		</view>
		<password v-if="show" :gasPrice="100" :yzpassword="i18n.yzpassword" :btnl="i18n.btn1" :btnr="i18n.btn2" :placeholder="i18n.placeholder" :passprice="i18n.passprice" :cap="i18n.cap" :gasLimit="1000000" @close="close" @callback="callback" :pwdtitle="i18n.pwdtitle" ></password>

	</view>
</template>

<script>
	import * as oex from 'oex-web3'; 
	import {myMixins} from '../../common/mixins.js'
	import password from '../../components/password/password.vue'
	// import BigNumber from 'bignumber.js';
	import payment from '../../common/payment.js'
	export default {
		computed: {
			i18n() {
				return this.$t('user')
			}
		},
		mixins: [myMixins], // 使用mixin
		data() {
			return {
				array: ['mineraccount1', 'mineraccount2', 'mineraccount3', 'mineraccount4'],
				index: 0,
				assetId:"",
				symbol:"",
				decimals:"",
				amount:"",
				
				address:"",
				outAmount:"",
				pwd:"",
				show:false,
				remark:"",   //转出备注
				titleimg:""
			}
		},
		components:{
			password
		},
		onLoad(options) {
			console.log(options)
			var _this=this
			this.accountName = this.account_info.accountName;
			this.assetId = Number(options.assetId);
			this.symbol = options.symbol;
			this.amount = options.amount;
			this.decimals = options.decimals;
			if(this.assetId==0){
				_this.titleimg="../../static/image/icon_detail.png"
			}else{
				_this.titleimg="../../static/image/greylogo.png"
			}
		},
		methods: {
			bindPickerChange: function(e) {
					//console.log('picker发送选择改变，携带值为', e.target.value)
					this.index = e.target.value
			},
			back(){
				this.$CommonJS.navigateBack()
			},
			all(){
				//全部转出
				this.outAmount = this.amount;
			},
			async outSubmit(){
				//验证
				const _this = this;
				if(!this.address){
					_this.$CommonJS.showToast(_this._i18n.locale == 'zh_CN' ? '地址不能为空':'Address cannot be empty');
					return;
				}
				if(this.outAmount == 0 || !this.outAmount){
					_this.$CommonJS.showToast(_this._i18n.locale == 'zh_CN' ? '数量不能为空':'Quantity cannot be empty');
					return;
				}
				if(this.outAmount > Number(this.amount)){
					_this.$CommonJS.showToast(_this._i18n.locale == 'zh_CN' ? '账户余额不足':'Insufficient account balance');
					return;
				}
				//验证转出的账户是否存在
				oex.utils.setApp();
				var data = await oex.account.getAccountExByName(this.address); //请求参数
				const nodeInfo = uni.getStorageSync('nodeInfo');
				this.$CommonJS.request(nodeInfo,"POST",data).then( res => {
						//console.log(res)
						if(res.data.result == null){
							_this.$CommonJS.showToast(_this._i18n.locale == 'zh_CN' ? "账户不存在":'Account does not exist')
							return;
						}
						//
						_this.show = true;
				}).catch(error => {
						console.log(error)
				})
			},
			close(){
				//关闭支付框
				this.show = false
			},
			callback(privateKey,gasPrice,gasLimit){
				const _this = this;
				// const _this = this;
				console.log(privateKey + "," + gasPrice + "," + gasLimit);
				//console.log(this.accountName,this.address,this.assetId,this.outAmount,this.decimals,privateKey,gasPrice,gasLimit)
				
				//支付交易参数(currentAccountName,toAccountName,assetId,amount,decimals,privateKey,gasPrice,gasLimit)
				//console.log(this.assetId, this.outAmount)
				payment.payment(this.accountName.trim(),this.address,Number(this.assetId),Number(this.outAmount),this.remark,Number(this.decimals),privateKey,gasPrice,gasLimit).then(res =>{
					//console.log(res)
					if(res){
						uni.showToast({
							title:_this._i18n.locale == 'zh_CN' ? "交易成功":'Successful trade',
						})
					}
				}).catch(err =>{
					console.log(err)
				})
				this.show = false
			}
			
		}
	}
</script>

<style lang="stylus">
	@import "../../static/stylus/wallet/wallet.stylus"
</style>
